package com.lashou.chunshen.lashoutuangou.nohttp;

import android.content.Context;

import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;


public class CallServer {

    private final RequestQueue mQueue;

    private CallServer(){
        mQueue = NoHttp.newRequestQueue();
    }

    private static CallServer callServer;


    public synchronized static CallServer getInstance(){
        if (callServer == null){
            callServer = new CallServer();
        }
        return callServer;
    }

    /**
     * 添加一个请求到队列中的
     * @param context
     * @param what
     * @param request
     * @param httpListner
     * @param canCancle
     * @param isLoading
     * @param <T>
     */
    public <T> void add(Context context, int what, Request<T> request
            ,HttpListner<T> httpListner,boolean canCancle,boolean isLoading){
        mQueue.add(what,request,new HttpResponseListner<T>(context,request,httpListner,canCancle,isLoading));
    }
    /**
     * 取消这个sign标记的所有请求
     */
    public void cancelBySign(Object sign) {
        mQueue.cancelBySign(sign);
    }

    /**
     * 取消队列中所有请求
     */
    public void cancelAll() {
        mQueue.cancelAll();
    }

    /**
     * 退出app时停止所有请求
     */
    public void stopAll() {
        mQueue.stop();
    }
}
